<?php

function editPar($inEvent, $sn, $status){

    settype($inEvent,"integer");
    settype($sn,"integer");
    
    $sql = sprintf("UPDATE event_%s SET paidStatus = %s WHERE SN = %s;",$inEvent,$status,$sn);
    $res = sendQ($sql);
    
    if($res){
        //SUCCES!
        return true;
    }else{
        //SOMETHING WENT WRONG
        return false;
    }
}

function getEmails($where = "members"){
	$res = sendQ("SELECT SN FROM " .$where);
	$out = "";
	while(($row = mysql_fetch_array($res))!=null){
		$out .= $row['SN'] . "@post.au.dk, ";
	}
	$out = substr($out,0,-2);
	return $out;
}

function addMember($name, $sn, $date){
	
	if(!checkSN($sn)){
                settype($sn,"integer");
                settype($date,"integer");
                
                $sql = sprintf("INSERT INTO members(SN,name,date) VALUES(%s,'%s',%s);",$sn,$name,$date);
                $res = sendQ($sql);
                
                if($res){
                    //SUCCES
                    return 1;
                }else{
                    //ERROR !!
                    return 0;
                }
	}else{
                //IS MEMBER
		return 2;
	}
}

function addEvent($title, $info, $max, $date){
	settype($date,"integer");
    settype($title,"string");
    settype($info,"string");
    settype($max,"integer");
    
    $sql = sprintf("INSERT INTO events (name,max,enddate,info) Values('%s',%s,%s,'%s');",$title,$max,$date,$info);
	echo $sql;
    $res = sendQ($sql);
    $sqlID = sprintf("SELECT ID FROM events WHERE name LIKE '%s'",$title);
    $resID = sendQ($sqlID);
    $resIDA = mysql_fetch_array($resID);
    $ID = $resIDA[0];
    createEventTables($ID);
    return $ID;
}

function createEventTables($id){
    settype($id,"integer");
    $eventList = sprintf("CREATE TABLE %s(".
                    "`SN` INT(8) NOT NULL,".
                    "`name` TEXT NOT NULL,".
                    "`paidStatus` BOOLEAN NOT NULL DEFAULT  '0'," .
                    "mkey MEDIUMTEXT NOT NULL,".
                    "PRIMARY KEY(SN))".
                    "ENGINE = InnoDB;",
                  'event_' . $id);
    $eventWaitList = sprintf("CREATE TABLE %s(" .
    				"ID INT(4) NOT NULL AUTO_INCREMENT,".
                    "`SN` INT(8) NOT NULL,".
                    "`name` TEXT NOT NULL,".
                    "mkey MEDIUMTEXT NOT NULL,".
                    "PRIMARY KEY(ID))".
                    "ENGINE = InnoDB;",
                  'event_' . $id. "_waitlist");
    sendQ($eventList);
    sendQ($eventWaitList);
}


function newSpot($inEvent){
	settype($inEvent,"integer");
	
	$sql = sprintf("SELECT * FROM event_%s_waitlist ORDER BY ID;",$inEvent);
	$res = sendQ($sql);
	if($res){
		$newPar = mysql_fetch_array($res);
		
		$newParSN = $newPar['SN'];
		
		$a = addPar($inEvent,$newParSN);

		if($a == 3){
			//Adding went OK
			$d = deletePar($inEvent,$newParSN,true);
			if($d){
				//Deleting went OK and
				return true;
			}else{
				//Deleting went NOT 
				return false;
			}
		}else{
			//ADDING WENT WRONG!
			return false;
		}
		
		
	}
	return false;
}


function paidMemberShip($sn){
	settype($sn,"integer");
	$sql = sprintf("SELECT * FROM pendingmembers WHERE SN = %s;",$sn);
	$res = sendQ($sql);
	if($res){
		//SUCCES
		$memberInf = mysql_fetch_array($res);
		$name = $memberInf['name'];
		$sql = sprintf("DELETE FROM pendingmembers WHERE SN = %s;",$sn);
		$res = sendQ($sql);
		if($res){
			//SUCCES
			$date = getdate();
			$e = addMember($name,$sn,$date[0]);
			if($e){
				//SUCCESS
				return 1;
			}
		}
	}
	return 0;
}

function isSN($sn){
	$sn = intval($sn);
	$size = strlen((string)$sn);
	if($size == 8){
		return true;
	}else{
		return false;
	}
}

function checkArrays($a,$b){                                                    //WORKING
	
	$countA = count($a);
	$countB = count($b);
	
	if($countA > 0 && $countB > 0){
		foreach($a as $aSN){
			foreach($b as $bSN){
				if($bSN == $aSN){
					return true;
				}
			}
		}
	}
	
	return false;
}
?>